package com.service;
import static com.def.GenericDef.ACTION_FLG_ADD;
import static com.def.GenericDef.ACTION_FLG_DELETE;
import static com.def.GenericDef.ACTION_FLG_QUERY;
import static com.def.GenericDef.ACTION_FLG_UPDATE;
import static com.def.GenericDef.E_SPCGRPDT_ALREDY_EXIST;
import static com.def.GenericDef.E_SPCGRPDT_NOT_FOUND_DT;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.ISpcBisGrpDtDao;
import com.icim.service.ICIMBaseService;
import com.model.Spc_bis_grp_dt;
import com.model.Spc_bis_grp_dtId;
import com.tx.Spcgrpdt.SpcgrpdtI;
import com.tx.Spcgrpdt.SpcgrpdtIA;
import com.tx.Spcgrpdt.SpcgrpdtO;
import com.tx.Spcgrpdt.SpcgrpdtOA;
import com.util.MqOpe;

@Scope("prototype")
@Service("SpcgrpdtService")
public class SpcgrpdtService extends ICIMBaseService{
	
	@Autowired
	private SpcgrpdtI spcgrpdtI;
	
	@Autowired
	private SpcgrpdtO spcgrpdtO; 
	
	
	@Autowired
	private ISpcBisGrpDtDao spcBisGrpDtDao;
	
	private Logger logger = Logger.getLogger(SpcgrpdtService.class);
	
	@Autowired
	private MqOpe mqOpe;
	public String subMainProc(String strInMsg){
		logger.info("InTrx is "+ strInMsg );
		Session session = spcBisGrpDtDao.getCrSession();
		try{
			spcgrpdtI = (SpcgrpdtI) mqOpe.inStr2Obj(strInMsg,spcgrpdtI);
			char action_flg = spcgrpdtI.getAction_flg().charAt(0);
			switch( action_flg ){
				case ACTION_FLG_QUERY   :  queryByCondition()  ; break;   
				case ACTION_FLG_ADD     :  addFnc()            ; break;
				case ACTION_FLG_UPDATE  :  updateFnc()         ; break;
				case ACTION_FLG_DELETE  :  deleteFnc()         ; break;
				default                 :  errActionFnc();   
			}
			if( RETURN_CODE_OK.equals(spcgrpdtO.getRtn_code()) ){
				session.flush();
				spcgrpdtO.setRtn_code(RETURN_CODE_OK);
				spcgrpdtO.setRtn_mesg(RETURN_MESG_OK);
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		
		}catch(Exception ex){
			spcgrpdtO.setTrx_id("SPCGRPDT");
			spcgrpdtO.setType_id("O");
			spcgrpdtO.setRtn_code(RETURN_CODE_UNKNOWN);
			spcgrpdtO.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}finally{
			logger.info(mqOpe.obj2Str(spcgrpdtO));
		}
		return mqOpe.obj2Str(spcgrpdtO);
	}
	public void addFnc()  {
		SpcgrpdtIA iary = spcgrpdtI.getIary().get(0);
		Spc_bis_grp_dtId id = new Spc_bis_grp_dtId();
		
//		String hql = "FROM Spc_bis_grp_dt where id.col_typ_fk='"+ iary.getCol_typ_fk()
//		             +"' and id.grp_no_fk='"+ iary.getGrp_no_fk() +"'";
//		Session session = spcBisGrpDtDao.getCrSession();
//		Query query = session.createQuery(hql);
//		
//		Spc_bis_grp_dt query_spc_bis_grp_dt = (Spc_bis_grp_dt)query.uniqueResult();
//		if(query_spc_bis_grp_dt==null){
//			return ;
//		}
		id.setCol_typ_fk(iary.getCol_typ_fk());
		id.setSub_grp_no(iary.getSub_grp_no());
		id.setGrp_no_fk(iary.getGrp_no_fk());
		Spc_bis_grp_dt spc_bis_grp_dt = spcBisGrpDtDao.get(id);
		if(spc_bis_grp_dt==null){
			spc_bis_grp_dt = new Spc_bis_grp_dt();
		}else{
			spcgrpdtO.setRtn_code(E_SPCGRPDT_ALREDY_EXIST+"");
			spcgrpdtO.setRtn_mesg("sub group already exist");
		}
		
		spc_bis_grp_dt.setId(id);
		spc_bis_grp_dt.setTool_id_fk(iary.getTool_id_fk());
		spc_bis_grp_dt.setCond_typ(iary.getCond_typ());
		spc_bis_grp_dt.setMdl_cate_fk(iary.getMdl_cate_fk());
		spc_bis_grp_dt.setMdl_id_fk(iary.getMdl_id_fk());
		spc_bis_grp_dt.setOpe_id_fk(iary.getOpe_id_fk());
//		spc_bis_grp_dt.setOpe_no_fk(iary.getOpe_no_fk());//TODO:
		spc_bis_grp_dt.setOpe_no_fk("*");
//		spc_bis_grp_dt.setOpe_ver_fk(iary.getOpe_ver_fk());//TODO:
		spc_bis_grp_dt.setOpe_ver_fk("*");//
//		spc_bis_grp_dt.setProc_id(iary.getProc_id());//TODO
		spc_bis_grp_dt.setProc_id("*");
		spc_bis_grp_dt.setPath_id_fk(iary.getPath_id_fk());
//		spc_bis_grp_dt.setPath_ver_fk(iary.getPath_ver_fk());//TODO:
		spc_bis_grp_dt.setPath_ver_fk("*");
//		spc_bis_grp_dt.setRecipe_id_fk(iary.getRecipe_id_fk());//TODO:
		spc_bis_grp_dt.setRecipe_id_fk("*");
		spc_bis_grp_dt.setValid_flg(iary.getValid_flg());
		spc_bis_grp_dt.setCus_id_fk(iary.getCus_id_fk());
		spc_bis_grp_dt.setWo_id_fk(iary.getWo_id_fk());
		spcBisGrpDtDao.save(spc_bis_grp_dt);
		spcgrpdtO.setRtn_code(RETURN_CODE_OK);
	}
	public void updateFnc(){
		
		List<SpcgrpdtIA> iaryList = spcgrpdtI.getIary();
		for(SpcgrpdtIA iary: iaryList){
			Spc_bis_grp_dtId id = new Spc_bis_grp_dtId();
			id.setCol_typ_fk(iary.getCol_typ_fk());
			id.setGrp_no_fk(iary.getGrp_no_fk());
			id.setSub_grp_no(iary.getSub_grp_no());
			Spc_bis_grp_dt spc_bis_grp_dt = spcBisGrpDtDao.get(id);
			if(spc_bis_grp_dt==null){
				spcgrpdtO.setRtn_code(E_SPCGRPDT_ALREDY_EXIST+"");
				spcgrpdtO.setRtn_mesg("not found subgroup");
				return ;
			}
			if(iary.getTool_id_fk()!=null){
				spc_bis_grp_dt.setTool_id_fk(iary.getTool_id_fk());
			}
			if(iary.getMdl_cate_fk()!=null){
				spc_bis_grp_dt.setMdl_cate_fk(iary.getMdl_cate_fk());
			}
			if(iary.getMdl_id_fk()!=null){
				spc_bis_grp_dt.setMdl_id_fk(iary.getMdl_id_fk());
			}
			if(iary.getMdl_typ_fk()!=null){
				spc_bis_grp_dt.setMdl_typ_fk(iary.getMdl_typ_fk());
			}
			if(iary.getPath_id_fk()!=null){
				spc_bis_grp_dt.setPath_id_fk(iary.getPath_id_fk());
			}
			if(iary.getPath_ver_fk()!=null){
				spc_bis_grp_dt.setPath_ver_fk(iary.getPath_ver_fk());
			}
			if(iary.getOpe_id_fk()!=null){
				spc_bis_grp_dt.setOpe_id_fk(iary.getOpe_id_fk());
			}
			if(iary.getOpe_no_fk()!=null){
				spc_bis_grp_dt.setOpe_no_fk(iary.getOpe_no_fk());
			}
			if(iary.getOpe_ver_fk()!=null){
				spc_bis_grp_dt.setOpe_ver_fk(iary.getOpe_ver_fk());
			}
			if(iary.getRecipe_id_fk()!=null){
				spc_bis_grp_dt.setRecipe_id_fk(iary.getRecipe_id_fk());
			}
			if(iary.getProc_id()!=null){
				spc_bis_grp_dt.setProc_id(iary.getProc_id());
			}
			if(iary.getCond_typ()!=null){
				spc_bis_grp_dt.setCond_typ(iary.getCond_typ());
			}
			if(iary.getCus_id_fk()!=null){
				spc_bis_grp_dt.setCus_id_fk(iary.getCus_id_fk());
			}
			if(iary.getWo_id_fk()!=null){
				spc_bis_grp_dt.setWo_id_fk(iary.getWo_id_fk());
			}
			spcBisGrpDtDao.update(spc_bis_grp_dt);
		}
		spcgrpdtO.setRtn_code(RETURN_CODE_OK);
		
	}
	public void deleteFnc(){
		List<SpcgrpdtIA> iaryList = spcgrpdtI.getIary();
		for(SpcgrpdtIA iary: iaryList){
			Spc_bis_grp_dtId id = new Spc_bis_grp_dtId();
			id.setCol_typ_fk(iary.getCol_typ_fk());
			id.setGrp_no_fk(iary.getGrp_no_fk());
			id.setSub_grp_no(iary.getSub_grp_no());
			Spc_bis_grp_dt spc_bis_grp_dt = spcBisGrpDtDao.get(id);
			if(spc_bis_grp_dt==null){
				spcgrpdtO.setRtn_code(E_SPCGRPDT_NOT_FOUND_DT+"");
				spcgrpdtO.setRtn_mesg("not found subgroup");
				return ;
			}
			spcBisGrpDtDao.delete(spc_bis_grp_dt);
			
		}
		spcgrpdtO.setRtn_code(RETURN_CODE_OK);
	}
	public void queryByCondition(){
		String hql = "FROM Spc_bis_grp_dt where 1=1" ;
		SpcgrpdtIA iary = spcgrpdtI.getIary().get(0);
		if(iary.getCol_typ_fk()!=null){
			hql = hql +" and id.col_typ_fk='" +iary.getCol_typ_fk() +"'";
		}
		if(iary.getGrp_no_fk()!=null){
			hql = hql +" and id.grp_no_fk='" +iary.getGrp_no_fk() +"'";
		}
		if(iary.getSub_grp_no()!=null){
			hql = hql +" and id.sub_grp_no='" +iary.getSub_grp_no() +"'";
		}
		hql = hql + "ORDER BY id.col_typ_fk,id.grp_no_fk,id.sub_grp_no";
		List<Spc_bis_grp_dt> spcBisGrpDtList = spcBisGrpDtDao.find(hql);
		List<SpcgrpdtOA> oaryList = new ArrayList<SpcgrpdtOA>();
		for(Spc_bis_grp_dt spc_bis_grp_dt :spcBisGrpDtList){
			SpcgrpdtOA oary = new SpcgrpdtOA();
			oary.setCol_typ_fk(spc_bis_grp_dt.getId().getCol_typ_fk());
			oary.setGrp_no_fk(spc_bis_grp_dt.getId().getGrp_no_fk());
			oary.setSub_grp_no(spc_bis_grp_dt.getId().getSub_grp_no());
			oary.setCond_typ(spc_bis_grp_dt.getCond_typ());
			oary.setTool_id_fk(spc_bis_grp_dt.getTool_id_fk());
			oary.setMdl_cate_fk(spc_bis_grp_dt.getMdl_cate_fk());
			oary.setMdl_id_fk(spc_bis_grp_dt.getMdl_id_fk());
			oary.setMdl_typ_fk(spc_bis_grp_dt.getMdl_typ_fk());
			oary.setPath_id_fk(spc_bis_grp_dt.getPath_id_fk());
			oary.setPath_ver_fk(spc_bis_grp_dt.getPath_ver_fk());
			oary.setOpe_no_fk(spc_bis_grp_dt.getOpe_no_fk());
			oary.setOpe_id_fk(spc_bis_grp_dt.getOpe_id_fk());
			oary.setOpe_ver_fk(spc_bis_grp_dt.getOpe_ver_fk());
			oary.setProc_id(spc_bis_grp_dt	.getProc_id());
			oary.setCus_id_fk(spc_bis_grp_dt.getCus_id_fk());
			oary.setWo_id_fk(spc_bis_grp_dt.getWo_id_fk());
			oaryList.add(oary);
		}
		spcgrpdtO.setRtn_code(RETURN_CODE_OK);
		spcgrpdtO.setOary(oaryList);
		spcgrpdtO.setTbl_cnt(oaryList.size());
	}
	public void errActionFnc(){
		spcgrpdtO.setRtn_code("0000009");
		spcgrpdtO.setRtn_mesg("Invalid Action_flg");
	}
	
}

